//
//  AllListsViewController.swift
//  Checklists
//
//  Created by cygarm on 15/11/30.
//  Copyright © 2015年 Razeware. All rights reserved.
//

import UIKit

class AllListsViewController: UITableViewController, ListDetailViewControllerDelegate, UINavigationControllerDelegate
{
	var dataModel: DataModel!
	
	override func viewDidLoad() 
	{
		super.viewDidLoad()
	}
	
	override func viewWillAppear(animated: Bool) {
		super.viewWillAppear(animated)
		
		tableView.reloadData()
	}
	
	override func viewDidAppear(animated: Bool) {
		super.viewDidAppear(animated)
		
		navigationController?.delegate = self
		
		let index = dataModel.indexOfSelectedChecklist
//		if index != -1 {
		if index >= 0 && index < dataModel.lists.count {
			let checklist = dataModel.lists[index]
			performSegueWithIdentifier("ShowChecklist", sender: checklist)
		}
	}
	
	override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 
	{
		if segue.identifier == "ShowChecklist" {
			let controller = segue.destinationViewController as! ChecklistViewController
			controller.checklist = sender as! Checklist
		} else if segue.identifier == "AddChecklist" {
			let navigationController = segue.destinationViewController as! UINavigationController
			let controller = navigationController.topViewController as! ListDetailViewController
			
			controller.delegate = self
			controller.checklistToEdit = nil
		}
	}

	// MARK: Table view data source

	override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
	{
		return dataModel.lists.count
	}

	override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
	{
		let cellIdentifier = "Cell"
		var cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)
		if cell == nil {
			cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellIdentifier)
		}
		cell.textLabel!.text = "List \(indexPath.row)"

		let checklist = dataModel.lists[indexPath.row]
		cell.textLabel!.text = checklist.name
		cell.accessoryType = .DetailDisclosureButton
		
		let count = checklist.countUncheckedItems()
		if checklist.items.count == 0 {
			cell.detailTextLabel!.text = "(No Items)"
		} else if count == 0 {
			cell.detailTextLabel!.text = "All Done!"
		} else {
			cell.detailTextLabel!.text = "\(count) Remaining"
		}
		cell.imageView!.image = UIImage(named: checklist.iconName)
		
		return cell
	}

	override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 
	{
		dataModel.indexOfSelectedChecklist = indexPath.row
		let checklist = dataModel.lists[indexPath.row]
		performSegueWithIdentifier("ShowChecklist", sender: checklist)
	}
	
	override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
		dataModel.lists.removeAtIndex(indexPath.row)
		
		let indexPaths = [indexPath]
		tableView.deleteRowsAtIndexPaths(indexPaths, withRowAnimation: .Automatic)
	}
	
	override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
		let navigationController = storyboard!.instantiateViewControllerWithIdentifier("ListNavigationController") as! UINavigationController
		let controller = navigationController.topViewController as! ListDetailViewController
		controller.delegate = self
		
		let checklist = dataModel.lists[indexPath.row]
		controller.checklistToEdit = checklist
		
		presentViewController(navigationController, animated: true, completion: nil)
	}
	
	//MARK: ListDetailViewControllerDelegate
	func listDetailViewControllerDidCancel(controller: ListDetailViewController) {
		dismissViewControllerAnimated(true, completion: nil)
	}
	
	func listDetailViewController(controller: ListDetailViewController, didFinishAddingChecklist checklist: Checklist) {
		dataModel.lists.append(checklist)
		dataModel.sortChecklists()
		tableView.reloadData()
		
		dismissViewControllerAnimated(true, completion: nil)
	}
	
	func listDetailViewController(controller: ListDetailViewController, didFinishEditingChecklist checklist: Checklist) 
	{
		dataModel.sortChecklists()
		tableView.reloadData()
		
		dismissViewControllerAnimated(true, completion: nil)
	}
	
	//MARK: UINavigationControllerDelegate
	func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
		if viewController === self {
			dataModel.indexOfSelectedChecklist = -1
		}
	}
}
